# -*- coding: utf-8 -*-

import pygeoip

from json import dumps

from django.http import HttpResponse
from django.http import HttpResponseRedirect

from django.template import RequestContext
from django.template.loader import get_template

from django.contrib.auth import get_user
from django.contrib.auth.decorators import login_required

from place.forms import *
from account.forms import *    

from forms import *
from models import *

from handle import _get_profile
from handle import _get_avatar_id
from handle import _ensure_location
from handle import _handle_mark_place
from handle import _handle_preference_home
from handle import _handle_preference_avatar
from handle import _handle_place_comment

from codename_bamboo.preference import JSON_MIME
#from codename_bamboo.preference import GEOIP_CITY_DATABASE
#geoip = pygeoip.GeoIP(GEOIP_CITY_DATABASE)


@login_required
def home(request):
    """
    Serve people request after signin successfully
    """
    username   = request.user.username
    template   = get_template('home.dtpl')
    avatar_id  = _get_avatar_id(username)
    place_form = PlaceForm()
    context    = RequestContext(request, {'username': username
                                        ,'avatar_id': avatar_id 
                                        ,'account': 'logout'
                                        ,'place_form': place_form})

    response = HttpResponse(template.render(context))

    request, response = _ensure_location(request, response)

    return response


@login_required
def mark(request, target=None):
    """
    Serve people's request for mark new place
    """
    username = request.user.username
    if request.method == 'GET':
        template  = get_template('action/mark.dtpl')
        avatar_id = _get_avatar_id(username)
        place_form = PlaceForm()
        context   = RequestContext(request, {'username': username
                                            ,'avatar_id': avatar_id 
                                            ,'account': 'logout'
                                            ,'place_form': place_form})

        response  = HttpResponse(template.render(context))

        request, response = _ensure_location(request, response, home_location=False)

        return response
    else:  # POST
         if not request.user.is_authenticated():
             print('is authen');
             return HttpResponseRedirect('/')
         else:
             print('handle');
             uid = _handle_mark_place(request)
             if uid:
                 return HttpResponseRedirect('/place/'+uid)
             else:
                 return HttpResponseRedirect('/')        


@login_required
def message(request, target=None):
    """
    Serve people's personal message request
    """
    username = request.user.username
    template  = get_template('action/message.dtpl')
    avatar_id = _get_avatar_id(username)
    place_form = PlaceForm()
    context   = RequestContext(request, {'username': username
                                        ,'avatar_id': avatar_id 
                                        ,'account': 'logout'
                                        ,'place_form': place_form})

    response  = HttpResponse(template.render(context))

    request, response = _ensure_location(request, response)

    return response                                                    


@login_required
def discuss(request, target=None):
    """
    Serve people's public discussing request
    """
    username = request.user.username
    template  = get_template('action/discuss.dtpl')
    avatar_id = _get_avatar_id(username)
    place_form = PlaceForm()
    context   = RequestContext(request, {'username': username
                                        ,'avatar_id': avatar_id 
                                        ,'account': 'logout'
                                        ,'place_form': place_form})

    response  = HttpResponse(template.render(context))

    request, response = _ensure_location(request, response)

    return response


@login_required
def preference(request, target=None):
    """
    Serve people's various preference request
    """
    print(request.method)
    if request.method == 'GET':
        username = request.user.username
        template  = get_template('action/preference.dtpl')
        avatar_id = _get_avatar_id(username)
        context   = RequestContext(request, {'username': username
                                            ,'avatar_id': avatar_id 
                                            ,'account': 'logout'
                                            })

        response  = HttpResponse(template.render(context))

        request, response = _ensure_location(request, response)

        return response

    else:
        print(request.POST)
        print(target)
        if not target:
            return HttpResponseRedirect('/home/')
        else:
            if target == 'home':
                results = _handle_preference_home(request)
                return HttpResponse(results, mimetype=JSON_MIME)
            elif target == 'avatar':
                success = _handle_preference_avatar(request)
                if success:
                    return HttpResponseRedirect('/home/')
                else:
                    return HttpResponseRedirect('/action/preference/')


@login_required
def profile(request, username):
    """
    Serve people's request to view other people's profile
    """
    if request.method == 'POST':
        return HttpResponseRedirect('/')
    else:
        profile = _get_profile(username)
        template = get_template('action/profile.dtpl')

        context = RequestContext(request, {'profile':profile})
        response = HttpResponse(template.render(context))

        return response


@login_required
def place_comment(request, uid):
    """
    Serve people's request for comment on a specific place
    """
    if request.method == 'GET':
        return HttpResponseRedirect('/')
    else:  # POST
        print('post')
        success = _handle_place_comment(request, uid)
        if success:
            return HttpResponseRedirect('/place/' + uid)
        else:
            return HttpResponseRedirect('/')
